#include "IDBSession.h"
#include "DBFactory.h"
#include "../FilterSDK/FilterSDK.h"

int main()
{
	IDBSession* pSession = CDBFactory::CreateSession(EDBServerTypes::IA_TAG_DB_SERVER_TYPE_SQLITE);

	CDBConfig config;
	config.DBName = "D:\\test.db";

	int status = pSession->Connect(config);
	STATUS_CHECK

	status = pSession->ExcuteNonQuery("CREATE TABLE IF NOT EXISTS algo_result( \
id INTEGER PRIMARY KEY,\
create_time INTEGER,\
sn1 TEXT,\
result1 REAL,\
memo TEXT);");
	STATUS_CHECK

		status = pSession->ExcuteNonQuery("CREATE TABLE IF NOT EXISTS users( \
id INTEGER PRIMARY KEY,\
create_time INTEGER,\
sn1 TEXT,\
result1 REAL,\
memo TEXT);");
	STATUS_CHECK

	std::vector<std::string> tables;
	status = pSession->GetTables(tables);
	STATUS_CHECK

	status = pSession->ExcuteNonQuery("INSERT INTO algo_result(create_time,sn1,result1,memo) \
VALUES(100, 'barcode12345678', 1.1234, 'this is a memo');");
	STATUS_CHECK

	status = pSession->BeginTransaction();
	STATUS_CHECK
	status = pSession->ExcuteNonQuery("INSERT INTO algo_result(create_time,sn1,result1,memo) \
VALUES(101, 'barcode2456789', 2.1234, 'this is a memo 2');");
	if (status != IA_L_OK) {
		pSession->RollBackTransaction();
		IA_RETURN_ERR
	}
	status = pSession->CommitTransaction();
	STATUS_CHECK

	std::string cnt;
	status = pSession->ExecuteScalar("SELECT count(*) FROM algo_result;", cnt);
	STATUS_CHECK

	int row = 0; 
	int col = 0;
	std::vector<std::string> results;
	status = pSession->ExcuteSelect("SELECT id, create_time, sn1, result1, memo FROM algo_result;", results, col, row);
	STATUS_CHECK

	status = pSession->Close();
	STATUS_CHECK

	IA_RETURN_OK
}